home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5045 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  67 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: sscanf bug??????
  5. Date: 10 Feb 1996 23:24:58 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb10162458@qcd.lanl.gov>
  8. References: <4fimvo$82s@fnord.dfw.net>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: jtmcap@dfw.dfw.net's message of 10 Feb 1996 18:08:56 GMT
  13.  
  14. In article <4fimvo$82s@fnord.dfw.net> jtmcap@dfw.dfw.net (Jerry
  15. Jackson) writes: 
  16. <snip>
  17.    #include <stdio.h>
  18.    #include <string.h>
  19.  
  20.    main()
  21.    {
  22.        char str_1[] = "013196";
  23.        char str_2[] = "13196";
  24.        long res_1, res_2;
  25.  
  26.        sscanf(str_1,"%d",&res_1);
  27.        sscanf(str_2,"%d",&res_2);
  28.  
  29.        printf("\nres_1 = %d",res_1);
  30.        printf("\nres_2 = %d",res_2);
  31.    }
  32.  
  33.    the output looks like this:
  34.  
  35.    res_1 = 89
  36.    res_2 = 13196
  37.  
  38.  
  39.    microway says that the leading zero causes sscanf to do an octal 
  40.    conversion on the integer.  i have not found any documentation to verify 
  41.    this.  also other compilers that i use return the value 13196 for both 
  42.    calls to sscanf.
  43.  
  44.    bug or undocumented feature?
  45.  
  46. Difficult to say. Your code invokes undefined behaviour: %d expects an
  47. int*, but you are passing it a long*. Once you invoke undefined
  48. behaviour, anything is allowed to happen. Similarly, in the printf, %d
  49. expects an int and you are passing it a long.
  50.  
  51. However, microway is definitely wrong. If after changing the
  52. declaration of res1 and res2, your result persists; the compiler is
  53. buggy beyond doubt.
  54.  
  55. Note that if you use %i in the sscanf, you are supposed to get the
  56. behaviour you see.
  57.  
  58. Cheers
  59. Tanmoy
  60. --
  61. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  62. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  63. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  64. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  65. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  66. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  67.